<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/29
 * Time: 14:13
 */

namespace Admin\Controller;

use Admin\Model\News;
use Admin\Model\NewsCategories;
use Common\Common\FileSystem;
use Common\Common\Pagination;
use Common\Common\ResponseResult;
use Common\Common\Validator;

class NewsController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkPermission('news-edit');
    }

    /**
     * 新闻分栏
     */
    public function newsCategories()
    {
        //实例化数据库
        $model = M('new_categories');
        $date = $model->select();

        //绑定数据
        $this->assign('date', $date);
        //解析模板
        $this->display('admin/news/Categories');
    }

    /**
     * 增加分栏
     */
    public function categoriesAdd()
    {
        if (!IS_POST) {
            $this->error('请求错误');
        }
        $v = Validator::make($_POST, [
            'title' => ['required', 'max:255', 'min:1'],
        ]);
        if ($v->filed()) {
            ResponseResult::error(1000, $v->error());
            $this->error('字段不正确');
        }
        $model = M('new_categories');
        $model->create();
        $model->add();
        $this->success('创建成功');
    }

    /**
     * 栏目更改
     */
    public function categories_update()
    {

        $id = $_GET['id'];
        $model = M('new_categories');

        $data = $model->select();
        $this->assign('date', $data);
        $this->assign('id', $id);

        $this->display('admin/news/categories');

    }

    public function categories_save()
    {
        $id = $_POST['id'];
        $model = M('new_categories');

        $info = $_POST;
        $model->where("id = $id")->save($info);
        $this->success('修改栏目成功');
    }

    /**
     * 栏目删除
     */
    public function categories_delete()
    {
        $id = $_GET['id'];
        $model = M('new_categories');
        $model->where("id=$id")->delete();
        $this->success('删除成功');
    }

    /**
     * 新闻列表和搜索
     */
    public function newsList()
    {
        $validator = Validator::make($_REQUEST, [
            'search' => ['min:1'],
            'page' => ['integer'],
            'id' => 'exists:news,id'
        ]);
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $search = $_REQUEST['search'];
        $id = I('get.id', 0);
        if ($validator->filed()) {
            if (isset($validator->error()['search'])) {
                $search = null;
            }
            if (isset($validator->error()['page'])) {
                $page = 1;
            }
            if (isset($validator->error()['id'])) {
                $id = null;
            }
        }
        $where = null;
        if ($search !== null) {
            $where = "title like '%$search%'";
        }
        $news = Pagination::make($page, 3, M('news'), $where);
        foreach ($news->data as $key => $item) {
            $news->data[$key]['category'] =
                NewsCategories::findById($news->data[$key]['new_categories_id'])['title'];
        }
        $categories = NewsCategories::all();
        $new = M('news')->where('id = ' . $id)->find();
        $this->assign('news', $news);
        $this->assign('search', $search);
        $this->assign('data', $news->data);
        $this->assign('categories', $categories);
        $this->assign('new', $new);
        $this->display('admin/news/list');
    }

    /**
     * 新闻添加
     */
    public function newsAdd()
    {
        $info = M('new_categories');
        $model = M('news');
        $data = $model->add();
        $datas = $info->select();

        $this->assign('datas', $datas);
        $this->assign('data', $data);
        $this->assign('name', 'tom');
        $this->display('admin/news/add');
    }

    /**
     * 插入新闻数据
     */
    public function newsSave()
    {

        if (!IS_POST) {
            $this->error('请求错误');
        }
        $request = array_merge($_POST, $_FILES);
        $data = array_only($request, ['new_categories_id', 'content', 'title']);
        if (isset($request['image'])) {
            $data['image'] = FileSystem::uploadFile('image');
        }
        if (News::insert($data)) {
            $this->success('添加成功', U('list'), 2);
        } else {
            $this->error('保存失败');
        }
    }

    /**
     * 新闻更新
     */
    public function new_update()
    {
        $model = M('news');
        $id = $_POST['id'];

        $data = $model->select();
        $info = $model->where("id = $id")->find();
        $this->assign('info', $info);

        $this->assign('data', $data);
        $this->assign('id', $id);
        $this->display('admin/news/list');
    }

    public function updates()
    {
        $model = M('news');
        $id = $_POST['id'];
        $info = array_merge($_POST,$_FILES);
        $info['image'] = FileSystem::uploadFile('image');
        $model->where("id=$id")->save($info);
        $this->success('修改成功');
    }

    /**
     * 新闻删除
     */
    public function new_delete()
    {
        $id = $_GET['id'];

        $model = M('news');
        $model->where("id = $id")->delete();
        $this->success('删除成功');

    }


}

